package com.android.svmc.app.mobile.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.svmc.app.mobile.activity.R;
import com.android.svmc.app.mobile.data.DatabaseSource;
import com.android.svmc.app.mobile.model.DiaryModel;
import com.android.svmc.app.mobile.view.FontTextView;

public class DiaryListAdapter extends BaseAdapter{
	Context mcontext;
	private LayoutInflater mInflater;
	ArrayList<DiaryModel> listdiary = new ArrayList<DiaryModel>();
	int mposition;
	View convertview;
	public DiaryListAdapter(Context context, ArrayList<DiaryModel> diaries) {
		mcontext = context;
		listdiary = diaries;
		mInflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {

		return listdiary.size();
	}

	@Override
	public Object getItem(int position) {

		return listdiary.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		final DiaryModel diary = listdiary.get(position);
		DiaryHolder holder;
		if (view == null){
			mInflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = mInflater.inflate(R.layout.item_diary, null);
			holder = new DiaryHolder();

			holder.title =(TextView)view.findViewById(R.id.item_diary_txt_title);
			holder.content =(TextView)view.findViewById(R.id.item_diary_txt_content);
			holder.date =(TextView)view.findViewById(R.id.item_diary_txt_date);
			holder.avatar =(ImageView)view.findViewById(R.id.item_diary_img_avatar);
			
			view.setTag(holder);
		}else {
			holder = (DiaryHolder)view.getTag();
		}
		holder.title.setText(diary.getTitle());
		holder.date.setText(diary.getDate());
		holder.content.setText(diary.getContent());
		FontTextView.setfontFlamenco(holder.content, mcontext);
		
		return view;
	}
	public void clear()
	{
	    listdiary.clear();
	}
	public void removeListItem(View rowView, int positon) {
        // TODO Auto-generated method stub
		final DiaryModel diary = listdiary.get(positon);
        final Animation animation = AnimationUtils.loadAnimation(rowView.getContext(), R.anim.splashfadeout);
        rowView.startAnimation(animation);
        Handler handle = new Handler();
        handle.postDelayed(new Runnable() {
      
            @Override
            public void run() {           	
                // TODO Auto-generated method stub
            	
            	DatabaseSource db = new DatabaseSource(mcontext);        	
				int rowdelete = db.deleteDiary(diary);
				notifyDataSetChanged();
            }
        },1000);

    }
	static class DiaryHolder{
		TextView title;
		TextView date;
		TextView content;
		ImageView avatar;
	}


}
